package vn.anhqn.englishsentensetranslator.utils;

import vn.anhqn.englishsentensetranslator.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TextView;

public final class DialogUtility {
	/**
	 * Show an alert dialog box
	 * 
	 * @param context
	 * @param message
	 */
	public static void alert(Context context, String message, int resTitleId) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(context.getString(resTitleId));
			alertDialog.setMessage(message);
			alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					arg0.dismiss();
				}
			});
			AlertDialog al = alertDialog.create();
			al.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
			if (b != null)
				b.setBackgroundResource(R.drawable.item_selector);
		}
	}

	/**
	 * Show an alert dialog box by messageId
	 * 
	 * @param context
	 * @param messageId
	 */
	public static void alert(Context context, int messageId) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			// alertDialog.setTitle(context.getString(R.string.title_dialog));
			alertDialog.setMessage(context.getString(messageId));
			alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					arg0.dismiss();
				}
			});
			AlertDialog al = alertDialog.create();
			al.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
			if (b != null)
				b.setBackgroundResource(R.drawable.item_selector);
		}
	}

	/**
	 * Show an alert dialog box by messageId
	 * 
	 * @param context
	 * @param messageId
	 */
	public static void alert(Context context, String message) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			// alertDialog.setTitle(context.getString(R.string.title_dialog));
			alertDialog.setMessage(message);
			alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					arg0.dismiss();
				}
			});
			AlertDialog al = alertDialog.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
            if(al != null){
			((TextView) al.findViewById(android.R.id.message)).setGravity(Gravity.CENTER_HORIZONTAL
					| Gravity.CENTER_VERTICAL);
            }
		}
	}

	/**
     * Show an alert dialog box by messageId
     * 
     * @param context
     * @param messageId
     */
    public static AlertDialog alert1(Context context, String message) {
        if (context != null) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
            // alertDialog.setTitle(context.getString(R.string.title_dialog));
            alertDialog.setMessage(message);
//            alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface arg0, int arg1) {
//                    arg0.dismiss();
//                }
//            });
            AlertDialog al = alertDialog.show();
            Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
            if(al != null){
            ((TextView) al.findViewById(android.R.id.message)).setGravity(Gravity.CENTER_HORIZONTAL
                    | Gravity.CENTER_VERTICAL);
            }
            return al;
        }
        return null;
    }

	/**
	 * Show an alert dialog box by String message
	 * 
	 * @param context
	 * @param message
	 * @param onOkClick
	 */
	public static void alert(Context context, String message, DialogInterface.OnClickListener onOkClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(R.string.app_name);
			alertDialog.setMessage(message);
			if (onOkClick != null) {
				alertDialog.setPositiveButton(R.string.ok, onOkClick);
			} else {
				alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						arg0.dismiss();
					}
				});
			}
			AlertDialog al = alertDialog.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}

	public static void alert(Context context, int title, String message) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(title);
			alertDialog.setMessage(message);
			alertDialog.setPositiveButton(R.string.ok, new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			AlertDialog al = alertDialog.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}

	public static void alert(Context context, int resTitle, int resMessage, DialogInterface.OnClickListener onOkClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(resTitle);
			alertDialog.setMessage(resMessage);
			alertDialog.setPositiveButton(R.string.ok, onOkClick);
			AlertDialog al = alertDialog.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}

	public static void alert(Context context, int resTitle, String resMessage, DialogInterface.OnClickListener onOkClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(resTitle);
			alertDialog.setMessage(resMessage);
			alertDialog.setPositiveButton(R.string.ok, onOkClick);
			AlertDialog al = alertDialog.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}

	/**
	 * Show an option yes/no dialog box
	 * 
	 * @param context
	 * @param messageId
	 * @param OkTextId
	 * @param cancelTextId
	 * @param onOKClick
	 */
	public static void showYesNoDialog(Context context, int resTitleId, int messageId, int OkTextId, int cancelTextId,
			final DialogInterface.OnClickListener onOKClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(context.getString(resTitleId));
			alertDialog.setMessage(context.getString(messageId));
			alertDialog.setNegativeButton(cancelTextId, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					dialog.dismiss();
				}
			});

			alertDialog.setPositiveButton(OkTextId, onOKClick);
			AlertDialog al = alertDialog.show();
			Button b = al.getButton(DialogInterface.BUTTON_NEGATIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}
	public static void showYesNoDialog(Context context, int messageId, int OkTextId, int cancelTextId,
			final DialogInterface.OnClickListener onOKClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			
			alertDialog.setMessage(context.getString(messageId));
			alertDialog.setNegativeButton(cancelTextId, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					dialog.dismiss();
				}
			});

			alertDialog.setPositiveButton(OkTextId, onOKClick);
			AlertDialog al = alertDialog.show();
			Button bP = al.getButton(DialogInterface.BUTTON_POSITIVE);
			Button bN = al.getButton(DialogInterface.BUTTON_NEGATIVE);
            if (bP != null){
                bP.setBackgroundResource(R.drawable.item_selector);
            }
            if (bN != null){
                bN.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}

	public static void showYesNoDialog(Context context, int resTitleId, int messageId, int OkTextId, int cancelTextId,
			final DialogInterface.OnClickListener onOKClick, final DialogInterface.OnClickListener onCancelClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(context.getString(resTitleId));
			alertDialog.setMessage(context.getString(messageId));
			alertDialog.setNegativeButton(cancelTextId, onCancelClick);
			alertDialog.setPositiveButton(OkTextId, onOKClick);
			AlertDialog al = alertDialog.show();
			Button bP = al.getButton(DialogInterface.BUTTON_POSITIVE);
			Button bN = al.getButton(DialogInterface.BUTTON_NEGATIVE);
            if (bP != null){
                bP.setBackgroundResource(R.drawable.item_selector);
            }
            if (bN != null){
                bN.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}

	/**
	 * @param mainActivity
	 * @param strNetworkErr
	 * @param strNoNetwork
	 */
	public static void alert(Context context, int strNetworkErr, int strNoNetwork) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(strNetworkErr);
			alertDialog.setMessage(strNetworkErr);
			alertDialog.setPositiveButton(R.string.ok, new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			AlertDialog al = alertDialog.show();
			Button b = al.getButton(DialogInterface.BUTTON_POSITIVE);
            if (b != null){
                b.setBackgroundResource(R.drawable.item_selector);
            }
		}
	}

	public static void showAlertWithTwoButton(final Context context, String title, String message,
			String positiveButtonText, DialogInterface.OnClickListener _positive_listener, String negativeButtonText,
			DialogInterface.OnClickListener _negative_listener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message);
		builder.setPositiveButton(positiveButtonText, _positive_listener);
		builder.setNegativeButton(negativeButtonText, _negative_listener);
		AlertDialog al = builder.show();
		Button bP = al.getButton(DialogInterface.BUTTON_POSITIVE);
		Button bN = al.getButton(DialogInterface.BUTTON_NEGATIVE);
        if (bP != null){
            bP.setBackgroundResource(R.drawable.item_selector);
        }
        if (bN != null){
            bN.setBackgroundResource(R.drawable.item_selector);
        }
        TextView messageText = (TextView) al.findViewById(android.R.id.message);
		if (messageText != null) {
			messageText.setGravity(Gravity.CENTER);
		}
	}

}
